<?php
/*
Extension Name: Blog
Extension Url: http://lussumo.com/addons/index.php?PostBackAction=AddOn&AddOnID=297
Description: Convert a Category into a blog
Version: 1.1.3
Author: Ziyad (MySchizoBuddy) Saeed
Author Url: N/A
*/
/*
 Changelog
  v1.1.3 IE 6 css issues fixed, convert png to gif, so IE6 can display transparent background
 v1.1.2 Social Bookmarks are now only icons and no text,  IE 7 css issues fixed
 v1.1 Fixed major bug in drafts mode
*/

// Blog Path
if (!defined('BLOG_PATH')) define('BLOG_PATH', dirname(__FILE__).'/');

//Define Global variables
global $Context, $Configuration;

// Blog Preferences
if( !array_key_exists('BLOG_CATEGORY_ID', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_CATEGORY_ID', '1');}
if( !array_key_exists('BLOG_TAB_POSITION', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_TAB_POSITION', '1');}
if( !array_key_exists('BLOG_SOCIALBOOKMARKS_DIGG', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_SOCIALBOOKMARKS_DIGG', '0');}
if( !array_key_exists('BLOG_SOCIALBOOKMARKS_DELICIOUS', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_SOCIALBOOKMARKS_DELICIOUS', '0');}
if( !array_key_exists('BLOG_SOCIALBOOKMARKS_STUMBLE', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_SOCIALBOOKMARKS_STUMBLE', '0');}
if( !array_key_exists('BLOG_SOCIALBOOKMARKS_TECHNORATI', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_SOCIALBOOKMARKS_TECHNORATI', '0');}
if( !array_key_exists('BLOG_PER_PAGE', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_PER_PAGE', '10');}
if( !array_key_exists('BLOG_DATE', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_DATE', 'j M y');}
if( !array_key_exists('BLOG_EXCLUDE_FROM_DISCUSSION', $Configuration)) {AddConfigurationSetting($Context, 'BLOG_EXCLUDE_FROM_DISCUSSION', '0');}


// Blog Draft Field
if (!array_key_exists('BLOG', $Configuration)) {
    	$Errors = 0;
	$query = "SHOW COLUMNS FROM ".$Configuration['DATABASE_TABLE_PREFIX']."Discussion LIKE `Draft` ";
	$result = mysql_query($query);
        if (!$result) {
		$query = "ALTER TABLE `".$Configuration['DATABASE_TABLE_PREFIX']."Discussion` ADD `Draft` ENUM('0','1') DEFAULT '0' NOT NULL ;";
                if (!mysql_query($query)) $Errors = 1;
        }

        if ($Errors == 0) {
            	AddConfigurationSetting($Context, 'BLOG', '1');
        	$Structure = "// Discussions Table Structure
\$DatabaseColumns['Discussion']['Draft'] = 'Draft';
";
                if (!AppendToConfigurationFile($Configuration['APPLICATION_PATH'].'conf/database.php', $Structure)) $Errors = 1;
        }
}

// Language files
$Context->SetDefinition('Blog', 'News Blog');
$Context->SetDefinition('BlogSettings', 'Blog Settings');
$Context->SetDefinition('BlogNotes', 'These settings are specific to the Blog extension. Choose the appropriate settings below.');
$Context->SetDefinition('BlogCategoryID', 'Select the category that you want formatted as a blog');
$Context->SetDefinition('BlogPostPerPage', 'How many blog posts per page');
$Context->SetDefinition('BlogSocialBookmarks', 'Show social bookmarking links');
$Context->SetDefinition('BlogSocialBookmarksDigg', 'Digg it');
$Context->SetDefinition('BlogSocialBookmarksDelicious', 'Del.icio.us');
$Context->SetDefinition('BlogSocialBookmarksStumble', 'Stumble Upon');
$Context->SetDefinition('BlogSocialBookmarksTechnorati', 'Technorati');
$Context->SetDefinition('Permalink', 'Permalink');
$Context->SetDefinition('BlogExclude', 'Blog posts should be included in discussion list');
$Context->SetDefinition('AllBlogPost', 'All Blog Posts');
$Context->SetDefinition('AllDraftBlogPost', 'All Draft Blog Posts');
$Context->SetDefinition('AddComments', 'Add Comment');
$Context->SetDefinition('BlogDate', 'Blog post date format');
$Context->SetDefinition('More', 'Click to read more...');
$Context->SetDefinition('BackToBlog', 'Back to Blog');
$Context->SetDefinition('Closed', '[Closed]');
$Context->SetDefinition('Draft', 'Draft blog post');
$Context->SetDefinition('Drafts', 'Drafts');
$Context->SetDefinition('DraftRoles', 'Draft Roles');
$Context->SetDefinition('DraftRolesNote', 'Select the roles that will be allowed to view Draft blog posts');
$Context->SetDefinition('Published', 'Published');
$Context->SetDefinition('PublishStatus', 'Blog Publish Status');
$Context->SetDefinition('PERMISSION_BLOG_DRAFT_VISIBLE', 'Can create/view/edit draft blog posts');

//Role Permissions
$Context->Configuration['PERMISSION_BLOG_DRAFT_VISIBLE'] = '0';

// Libraries
include(BLOG_PATH . '/library/Function.Blog.php');
include(BLOG_PATH . '/library/Control.Blog.php');
include(BLOG_PATH . '/library/Control.BlogPermalink.php');
include(BLOG_PATH . '/library/PostBackControl.BlogForm.php');

/**
 * Add a new tab
 */
global $Menu;
if (isset($Menu))
	$Menu->AddTab($Context->GetDefinition('Blog'), 'Blog', GetUrl($Configuration, 'extension.php', 'category/', 'CategoryID', $Context->Configuration['BLOG_CATEGORY_ID'], '',  'PostBackAction=Blog'), '', $Context->Configuration['BLOG_TAB_POSITION']);

/**
 * How to handle Draft blog posts based on permission and pages.
 */
if ($Context->Session->User->Permission('PERMISSION_BLOG_DRAFT_VISIBLE')
    && in_array($Context->SelfUrl ,array("extension.php","comments.php")) && ForceIncomingInt('Draft', 0) == 1){
        $Context->AddToDelegate("DiscussionManager","PostGetDiscussionBuilder","DiscussionManager_ShowDraft");
        $Context->AddToDelegate("DiscussionManager","PreGetDiscussionCount","DiscussionManager_ShowDraft");
}
else if ($Context->SelfUrl != "post.php") {
        $Context->AddToDelegate("DiscussionManager","PostGetDiscussionBuilder","DiscussionManager_ExcludeDraft");
        $Context->AddToDelegate("DiscussionManager","PreGetDiscussionCount","DiscussionManager_ExcludeDraft");
}

/**
 * Generate Blog Grid
 */
if ($Context->SelfUrl == 'extension.php' && ForceIncomingString('PostBackAction', '') == 'Blog' && ForceIncomingString('page', '') != 'Permalink') {
    // make the blog chronological
    $DatabaseColumns['Discussion']['DateLastActive'] = 'DateCreated';
    //Change definitions to be blog specific

    $Context->Dictionary['StartANewDiscussion'] = 'Start a new blog post';
    $Head->AddStyleSheet('extensions/Blog/theme/style.css');
    $Head->BodyId = 'Blog';
    if (isset($Menu)) $Menu->CurrentTab = 'Blog';
    $Context->PageTitle =  ForceIncomingInt('Draft', 0) ? $Context->GetDefinition('AllDraftBlogPost') : $Context->GetDefinition('AllBlogPost');
    $Blog = $Context->ObjectFactory->NewContextObject($Context, 'Blog');
    $Page->AddRenderControl($Blog, $Configuration['CONTROL_POSITION_BODY_ITEM']);

    // Who can view the Blog Status (Drafts, published) sidepanel
    if ($Context->Session->User->Permission('PERMISSION_BLOG_DRAFT_VISIBLE')) {
        $PublishStatus = $Context->GetDefinition("PublishStatus");
        $Panel->AddList($PublishStatus);
        $Panel->AddListItem($PublishStatus, $Context->GetDefinition("Drafts"),GetUrl($Configuration, 'extension.php', 'category/', 'CategoryID', $Context->Configuration['BLOG_CATEGORY_ID'], '',  'PostBackAction=Blog&Draft=1'));
        $Panel->AddListItem($PublishStatus, $Context->GetDefinition("Published"),GetUrl($Configuration, 'extension.php', 'category/', 'CategoryID', $Context->Configuration['BLOG_CATEGORY_ID'], '',  'PostBackAction=Blog'));
    }
}

/**
 * Generate Permalink Page
 */
if ($Context->SelfUrl == 'extension.php' && ForceIncomingString('PostBackAction', '') == 'Blog' && ForceIncomingString('page', '') == 'Permalink') {

    $Context->Dictionary['StartANewDiscussion'] = 'Start a new blog post';
    $Head->AddStyleSheet('extensions/Blog/theme/style.css');
    $Head->BodyId = 'BlogPermalink';
    if (isset($Menu)) $Menu->CurrentTab = 'Blog';
    $BlogPermalink = $Context->ObjectFactory->NewContextObject($Context, 'BlogPermalink');
    $Page->AddRenderControl($BlogPermalink, $Configuration['CONTROL_POSITION_BODY_ITEM']);

}
/**
 * Settings Panel. This is where all the forms are generated.
 */
if(($Context->SelfUrl == 'settings.php') && $Context->Session->User->Permission('PERMISSION_CHANGE_APPLICATION_SETTINGS')) {

	$BlogForm = $Context->ObjectFactory->NewContextObject($Context, 'BlogForm');
	$Page->AddRenderControl($BlogForm, $Configuration["CONTROL_POSITION_BODY_ITEM"]);
	$ExtensionOptions = $Context->GetDefinition('Extension Options');
	$Panel->AddList($ExtensionOptions);
	$Panel->AddListItem($ExtensionOptions, $Context->GetDefinition('BlogSettings'), GetUrl($Context->Configuration, 'settings.php', '', '', '', '', 'PostBackAction=Blog'));
}

/**
 * Category Hider by Wallphone
 */
if ( ('index.php' == $Context->SelfUrl) && !(ForceIncomingInt('CategoryID', '') == $Context->Configuration['BLOG_CATEGORY_ID']) && !$Context->Configuration['BLOG_EXCLUDE_FROM_DISCUSSION'] ) {
    $Context->AddToDelegate('DiscussionManager', 'PostGetDiscussionBuilder', 'Category_HideFromDiscussions');
    $Context->AddToDelegate('DiscussionManager', 'PreGetDiscussionCount', 'Category_HideFromDiscussions');
}

if ( ('index.php' == $Context->SelfUrl) && (ForceIncomingInt('CategoryID', '') == $Context->Configuration['BLOG_CATEGORY_ID'])) {
        $Context->Dictionary['StartANewDiscussion'] = 'Start a new blog post';
        $Context->AddToDelegate('Menu', 'PreHeadRender', 'ChangeCurrentTab');
}

/**
 * Draft mode and other small changes here and there to make the definitions appropriate for a blog. Also disable features that are not needed, like Whispers.
 */
if  ('post.php' == $Context->SelfUrl && ForceIncomingInt('CategoryID', '') == $Context->Configuration['BLOG_CATEGORY_ID'])  {
    $Context->Dictionary['EnterYourDiscussionTopic'] = 'Enter your blog title';
    $Context->Dictionary['StartANewDiscussion'] = 'Start a new blog post';
    $Context->Dictionary['StartYourDiscussion'] = 'Submit your blog post';
    $Context->Dictionary['SelectDiscussionCategory'] = '';
    $Head->BodyId = 'Blog';
    $Head->AddString('<STYLE TYPE="text/css" MEDIA=screen>.CategorySelect {display:none;}</STYLE>');
    $Context->Configuration['ENABLE_WHISPERS'] = '0';
    $Panel->AddListItem($Context->GetDefinition("Options"), $Context->GetDefinition("BackToBlog"), GetUrl($Context->Configuration, 'extension.php', 'category/', 'CategoryID', $Context->Configuration['BLOG_CATEGORY_ID'], '',  'PostBackAction=Blog'));
    $Context->AddToDelegate('Menu', 'PreHeadRender', 'ChangeCurrentTab');
    if ($Context->Session->User->Permission('PERMISSION_BLOG_DRAFT_VISIBLE')){ //check for user permission
        $Context->AddToDelegate('DiscussionForm', 'DiscussionForm_PreCommentRender', 'DiscussionForm_RenderDraft');
        $Context->AddToDelegate('DiscussionForm', 'PostSaveDiscussion', 'DiscussionForm_SaveDraft');
    }
}
/**
 * After a blog post is saved redirect to blog tab and not to discussions
 */
if  ('post.php' == $Context->SelfUrl)  {
    $Context->AddToDelegate('DiscussionForm', 'PostSaveDiscussion', 'DiscussionForm_BlogRedirect');
}
/**
 * Add latest blog post panel list
 *
if ($Context->Configuration['BLOG_ALLOW_LATEST_POST']
    && (in_array($Context->SelfUrl, array('index.php','comments.php')))
    || ($Context->SelfUrl == 'extension.php' && ForceIncomingString('PostBackAction', '') == 'BlogPermalink')) {
        $DiscussionManager = $Context->ObjectFactory->NewContextObject($Context, "DiscussionManager");
        //AddLatestBlogPostToPanel($Context, $Panel, $DiscussionManager);
}
 */
?>
